"ui";

ui.layout(
    <vertical>
        <linear>
            <Switch id="autoService" text="无障碍服务:" checked="{{auto.service != null}}" w="auto" layout_gravity="center" paddingLeft="0" />
            <Switch id="suspensionService" text="悬浮窗:" checked="{{auto.service != null}}" w="auto" layout_gravity="center" paddingRight="0" />
        </linear>
        <button id='btn_dy' text="抖音极速版"/>
        <button id='btn_ks' text="快手极速版"/>
        <button id='btn_bd' text="百度极速版"/>
    </vertical>
);

var common = require("../../common/common.js");
var ks = require("./ks.js");
var dy = require("./dy.js");
var baidu = require("./baidu.js");
const { resetConsole } = require("../../common/common.js");

ui.btn_dy.click(function() {
    common.thread(function () {
        console.show()
        dy.activeMoney()
    })
});
ui.btn_ks.click(function() {
    common.thread(function () {
        console.show()
        ks.activeMoney()
    })
});
ui.btn_bd.click(function() {
    common.thread(function () {
        console.show()
        baidu.activeMoney()
    })
});

ui.autoService.on("check", function(checked) {
    // 用户勾选无障碍服务的选项时，跳转到页面让用户去开启
    if(checked && auto.service == null) {
        app.startActivity({
            action: "android.settings.ACCESSIBILITY_SETTINGS"
        });
    }
    if(!checked && auto.service != null){
        auto.service.disableSelf();
    }
});

ui.suspensionService.on("check", function(checked) {
    //申请悬浮窗
    importClass(android.content.Intent);
    importClass(android.net.Uri);
    importClass(android.provider.Settings);
    var intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
        Uri.parse("package:" + context.getPackageName()));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    app.startActivity(intent);
});
// 当用户回到本界面时，resume事件会被触发
ui.emitter.on("resume", function() {
    // 此时根据无障碍服务的开启情况，同步开关的状态
    ui.autoService.checked = auto.service != null;
    ui.suspensionService.checked = floaty.checkPermission() != false
});

//监听音量键按下
events.onKeyDown("volume_up", () => {
    toastLog('按音量键停止');
    exit();
});